'''
Created on 11/07/2010

@author: Mar
'''
import unittest
from Inode import INode
from TripleIndirectionBlock import TripleIndirectionBlock
from DoubleIndirectionBlock import DoubleIndirectionBlock
from SimpleIndirectionBlock import SimpleIndirectionBlock


class InodeTest(unittest.TestCase):


    def setUp(self):
        self.inode = INode(1)
        self.tBlock = TripleIndirectionBlock()
        self.tBlock1 = TripleIndirectionBlock()
        self.tBlock2 = TripleIndirectionBlock()
        self.dBlock = DoubleIndirectionBlock()
        self.dBlock1 = DoubleIndirectionBlock()
        self.dBlock2 = DoubleIndirectionBlock()
        self.sBlock =  SimpleIndirectionBlock()
        self.sBlock1 = SimpleIndirectionBlock()
        self.sBlock2 = SimpleIndirectionBlock()
        self.sBlock3 = SimpleIndirectionBlock()
        self.dBlock1.addIndirectionBlock(self.sBlock2)
        self.tBlock1.addDoubleIndirectionBlock(self.dBlock2)
        self.dBlock2.addIndirectionBlock(self.sBlock3)
        self.inode.addTripleIndirectionBlock(self.tBlock1)
        self.inode.addDoubleIndirectionBlock(self.dBlock1)
        self.inode.addsimpleIndirectionBlock(self.sBlock1)
        self.inode.addDataPointer(1000)
        self.inode.addDataPointer(2000)
        self.inode.addDataPointer(3000)
        self.inode.addDataPointer(4000)
        self.sBlock1.addDataPointer(2345, 5)
        self.sBlock1.addDataPointer(2346, 6)
        self.dBlock1.addDataPointer(1001, 1)
        self.dBlock1.addDataPointer(1002, 2)
        self.dBlock1.addDataPointer(1003, 3)        
        self.dBlock1.addDataPointer(1004, 4)
        self.tBlock1.addDataPointer(7000, 10)        
        self.tBlock1.addDataPointer(7001, 11)        
        self.tBlock1.addDataPointer(7002, 12)        
        self.tBlock1.addDataPointer(7003, 13)        
        self.tBlock1.addDataPointer(7004, 14)        
        self.tBlock1.addDataPointer(7005, 15)        
        self.inode2 = INode(2)
        


    def tearDown(self):
        self.inode = None
        self.tBlock = None
        self.tBlock1 = None
        self.tBlock2 = None
        self.dBlock = None
        self.dBlock1 = None
        self.dBlock2 = None
        self.sBlock = None
        self.sBlock1 = None
        self.sBlock2 = None
        self.sBlock3 = None



    def testAddSimpleIndirectionBlock(self):
        self.inode.addsimpleIndirectionBlock(self.sBlock)
        self.assertTrue(self.inode.getSimpleBlocks().__contains__(self.sBlock))
        
    def testAddDoubleIndirectionBlock(self):
        self.inode.addDoubleIndirectionBlock(self.dBlock)
        self.assertTrue(self.inode.getDoubleBlocks().__contains__(self.dBlock))
        
    def testAddTripleIndirectionBlock(self):
        self.inode.addTripleIndirectionBlock(self.tBlock)
        self.assertTrue(self.inode.getTripleBlocks().__contains__(self.tBlock))
        
    def testAddDataPointer(self):
        self.inode.addDataPointer(5555)
        self.assertTrue(self.inode.getDataIndex().__contains__(5555))
        self.assertEqual(self.inode.getDataIndex().get(5555), 5)
        
    def testAddDataPointer1(self):
        #=======================================================================
        # Testeamos si no hay otro lugar en el data index, que se agregue en el primer simple block.
        #=======================================================================
        self.inode.addDataPointer(6666)
        self.inode.addDataPointer(7777)
        self.inode.addDataPointer(8888)
        self.inode.addDataPointer(9999)
        self.inode.addDataPointer(10000)
        self.assertTrue(self.sBlock1.getDataBlockPointers().__contains__(10000))
        self.assertEqual(self.sBlock1.getDataBlockPointers().get(10000), 9)
        
    def testSimpleIndirectionIndexHasSpace(self):
        self.assertTrue(self.inode.simpleIndirectionIndexHasSpace())
        
    def testDoubleIndirectionIndexHasSpace(self):
        self.assertTrue(self.inode.doubleIndirectionIndexHasSpace())
        
    def testTripleIndirectionIndexHasSpace(self):
        self.assertTrue(self.inode.tripleIndirectionIndexHasSpace())
        
    def testaddDataPointerToSimpleIndirectionBlocks(self):
        self.inode.addDataPointerToSimpleIndirectionBlocks(5000)
        self.assertTrue(self.sBlock1.getDataBlockPointers().__contains__(5000))
        self.assertEqual(self.sBlock1.getDataBlockPointers().get(5000), 5)
        
    def testAddDataPointerToDoubleIndirectionBlocks(self):
        self.inode.addDataPointerToDoubleIndirectionBlocks(1212)
        self.assertTrue(self.sBlock2.getDataBlockPointers().__contains__(1212))
        self.assertEqual(self.sBlock2.getDataBlockPointers().get(1212), 5)
        
    def testAddDataPointerToTripleIndirectionBlocks(self):
        self.inode.addDataPointerToTripleIndirectionBlocks(3434)
        self.inode.addDataPointerToTripleIndirectionBlocks(3435)
        self.assertTrue(self.sBlock3.getDataBlockPointers().__contains__(3434))
        self.assertEqual(self.sBlock3.getDataBlockPointers().get(3435), 6)
        
    
    def testSimpleIndexContains(self):
        self.sBlock1.addDataPointer(1122, 23)
        self.assertTrue(self.inode.simpleIndexContains(1122))
        self.assertFalse(self.inode.simpleIndexContains(6789))
        
    def testDoubleIndexContains(self):
        self.dBlock1.addDataPointer(2233, 34)
        self.assertTrue(self.inode.doubleIndexContains(2233))
        self.assertFalse(self.inode.doubleIndexContains(3355))
        
    def testTripleIndexContains(self):
        self.tBlock1.addDataPointer(3344, 45)
        self.assertTrue(self.inode.tripleIndexContains(3344))
        self.assertFalse(self.inode.tripleIndexContains(3232))
        
        
    def testContains(self):
        self.assertTrue(self.inode.contains(1000))
        self.assertTrue(self.inode.contains(2345))
        self.assertFalse(self.inode.contains(45634535423))
        
    def testRemoveDataPointerFromSimpleIndex(self):
        self.sBlock1.addDataPointer(3333, 3)
        self.inode.removeDataPointerFromSimpleIndex(3333)
        self.assertFalse(self.inode.contains(3333))
        self.assertFalse(self.sBlock1.contains(3333))
        
        
    def testRemoveDataPointerFromDoubleIndex(self):
        self.dBlock1.addDataPointer(4444, 4)
        self.inode.removeDataPointerFromDoubleIndex(4444)
        self.assertFalse(self.inode.contains(4444))
        self.assertFalse(self.dBlock1.contains(4444))
        
        
    def testRemoveDataPointerFromTripleIndex(self):
        self.tBlock1.addDataPointer(5555, 5)
        self.inode.removeDataPointerFromTripleIndex(5555)
        self.assertFalse(self.inode.contains(5555))
        self.assertFalse(self.tBlock1.contains(5555))
    
    def testRemoveDataPointer(self):
        self.inode.removeDataPointer(2345)
        self.assertFalse(self.inode.contains(2345))
        self.assertFalse(self.sBlock1.contains(2345))
    
    
    def testSimpleIndirectionHasSpaceToANewBlock(self):
        self.assertTrue(self.inode.simpleIndirectionIndexHasSpaceToANewBlock())
        
    def testTripleHasSpace(self):
        self.assertTrue(self.inode.tripleHasSpace())
        
    def testTripleIndexHasSpaceToASimpleBlock(self):
        self.assertTrue(self.inode.tripleIndexHasSpaceToASimpleBlock())
        
    def testTripleIndexHasSpaceToADoubleBlock(self):
        self.assertTrue(self.inode.tripleIndexHasSpaceToADoubleBlock())
    
    def testAddSimpleBlockToTripleIndirectionIndex(self):
        self.sBlock70 = SimpleIndirectionBlock()
        self.inode.addSimpleBlockToTripleIndirectionIndex(self.sBlock70)
        self.assertTrue(self.dBlock2.getPointers().__contains__(self.sBlock70))
        
    def testAddDoubleBlockToTripleIndirectionIndex(self):
        self.dBlock70 = DoubleIndirectionBlock()
        self.inode.addDoubleBlockToTripleIndirectionIndex(self.dBlock70)
        self.assertTrue(self.tBlock1.getPointers().__contains__(self.dBlock70))
        
    def testDoubleIndexHasSpaceToASimpleBlock(self):
        self.assertTrue(self.inode.doubleIndexHasSpaceToASimpleBlock())
    
    def testAddSimpleBlockToDoubleIndirectionIndex(self):
        self.sBlock80 = SimpleIndirectionBlock()
        self.inode.addSimpleBlockToDoubleIndirectionIndex(self.sBlock80)
        self.assertTrue(self.dBlock1.getPointers().__contains__(self.sBlock80))
    
    def testDoubleHasSpace(self):
        self.assertTrue(self.inode.doubleHasSpace())
        
    def testSimpleIndirectionIndexHasSpaceToANewBlock(self):
        self.assertTrue(self.inode.simpleIndirectionIndexHasSpaceToANewBlock())
    
    def testSetInode(self):
        self.list = [1000,2000,3000]
        self.inode2.setInode("The FileName", self.list)
        self.assertEqual(self.inode2.getDataIndex().__getitem__(2000), 2)
        self.assertEqual(self.inode2.getDataIndex().__getitem__(3000), 3)
        self.assertEqual(self.inode2.getDataIndex().__getitem__(1000), 1)
        self.assertEquals(self.inode2.getFileName(),"The FileName")
        
    def testSetInode2 (self):
        self.list1 = [123,122,111,133,134,144,177,178,199,200,210]
        self.inode3 = INode(2)
        self.inode3.setInode("Test", self.list1)
        self.assertEqual(self.inode3.getDataIndex().__getitem__(123),1)
        self.assertEqual(self.inode3.getDataIndex().__getitem__(122),2)
        self.assertEqual(self.inode3.getDataIndex().__getitem__(111),3)
        self.assertEqual(self.inode3.getDataIndex().__getitem__(133),4)
        self.assertEqual(self.inode3.getDataIndex().__getitem__(134),5)
        self.assertEqual(self.inode3.getDataIndex().__getitem__(144),6)
        self.assertEqual(self.inode3.getDataIndex().__getitem__(177),7)
        self.assertEqual(self.inode3.getDataIndex().__getitem__(178),8)
        self.assertEqual(self.inode3.getSimpleBlocks().__len__(), 1) #saber si se creo el simple
        self.assertEqual(len((self.inode3.getSimpleBlocks()[0]).getDataBlockPointers()),3) #los otros tres se agregaron al simple
        
    def testGetBlocksFromDataIndex(self):
        self.list1 = self.inode.getBlocksFromDataIndex()
        self.assertEqual(self.list1[0], 1000)
        self.assertEqual(self.list1[1], 2000)
        self.assertEqual(self.list1[2], 3000)
        self.assertEqual(self.list1[3], 4000)
        self.assertEqual(self.list1.__len__(), 4)
        
    def testGetBlocksFromSimpleIndex(self):
        self.list2 = self.inode.getBlocksFromSimpleIndex()
        self.assertEqual(self.list2[0], 2345)
        self.assertEqual(self.list2[1], 2346)
        self.assertEqual(self.list2.__len__(), 2)
        
    def testGetBlocksFromDoubleIndex(self):
        self.list3 = self.inode.getBlocksFromDoubleIndex()
        self.assertEqual(self.list3[0], 1001)
        self.assertEqual(self.list3[1], 1002)
        self.assertEqual(self.list3[2], 1003)
        self.assertEqual(self.list3[3], 1004)
        self.assertEqual(self.list3.__len__(), 4)
        
    def testGetBlocksFromTripleIndex(self):
        self.list4 = self.inode.getBlocksFromTripleIndex()
        self.assertEqual(self.list4[0], 7000)
        self.assertEqual(self.list4[1], 7001)
        self.assertEqual(self.list4[2], 7002)
        self.assertEqual(self.list4[3], 7003)
        self.assertEqual(self.list4[4], 7004)        
        self.assertEqual(self.list4[5], 7005)        
        self.assertEqual(self.list4.__len__(), 6)
        
    def testGetBlocks(self):
        self.list5 = self.inode.getBlocks()
        self.assertEqual(self.list5[0], 1000)
        self.assertEqual(self.list5[1], 2000)
        self.assertEqual(self.list5[2], 3000)
        self.assertEqual(self.list5[3], 4000)
        self.assertEqual(self.list5[4], 2345)
        self.assertEqual(self.list5[5], 2346)
        self.assertEqual(self.list5[6], 1001)
        self.assertEqual(self.list5[7], 1002)
        self.assertEqual(self.list5[8], 1003)
        self.assertEqual(self.list5[9], 1004)
        
        
    


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testAdd']
    unittest.main()
    
    
suite = unittest.TestLoader().loadTestsFromTestCase(InodeTest)
unittest.TextTestRunner(verbosity=5).run(suite)